package com.dhcy.mqtt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcy.mqtt.dao.*;
import com.dhcy.mqtt.model.*;
import com.dhcy.mqtt.par.ht.DeviceAddPar;
import com.dhcy.mqtt.par.ht.DeviceListPar;
import com.dhcy.mqtt.result.ht.DeviceResult;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.service.DeviceService;
import com.dhcy.mqtt.util.AppResultData;
import com.dhcy.mqtt.util.IsNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Autowired
    DeviceMapper deviceMapper;

    @Autowired
    DeviceClassMapper deviceClassMapper;

    @Autowired
    MqttSettingMapper mqttSettingMapper;

    @Autowired
    DeviceThemeMapper deviceThemeMapper;

    /**
     * 设备列表
     * @param deviceListPar
     * @return
     */
    @Override
    public TableResultData deviceList(DeviceListPar deviceListPar) {
        IPage<DeviceResult> page = new Page<>(deviceListPar.getPage(), deviceListPar.getPageSize());
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        if (!IsNull.isNullOrEmpty(deviceListPar.getName())) {
            queryWrapper.like("d.name", deviceListPar.getName());
        }
        if (!IsNull.isNullOrEmpty(deviceListPar.getCode())) {
            queryWrapper.like("d.code", deviceListPar.getCode());
        }
        List<DeviceResult> resultList = deviceMapper.list(page, queryWrapper);
        return TableResultData.success(page.getTotal(), resultList);
    }

    /**
     * 设备列表
     * @param deviceListPar
     * @return
     */
   @Override
   public AppResultData deviceListSimple(DeviceListPar deviceListPar){
       TableResultData tableResultData = deviceClassList(deviceListPar);
       return  AppResultData.success("成功",tableResultData.getRows());
   }


    /**
     * 新增设备
     * @param deviceaddPar
     * @return
     */
    @Override
    public AppResultData deviceAdd(DeviceAddPar deviceaddPar) {
        Device device = new Device();
        BeanUtils.copyProperties(deviceaddPar, device);
        if (IsNull.isNullOrEmpty(device.getId())) {
            device.setCreateTime(new Date());
            deviceMapper.insert(device);
        } else {
            device.setUpdateTime(new Date());
            deviceMapper.updateById(device);
        }
        return AppResultData.successMessage("成功");
    }

    /**
     * 删除设备
     * @param id
     * @return
     */
    @Override
    public AppResultData deviceDel(Integer id) {
        int count = deviceMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }

    /**
     * 设备类型列表
     * @param deviceListPar
     * @return
     */
    @Override
    public TableResultData deviceClassList(DeviceListPar deviceListPar) {
        IPage<DeviceClass> page = new Page<>(deviceListPar.getPage(), deviceListPar.getPageSize());
        QueryWrapper<DeviceClass> queryWrapper = new QueryWrapper<>();
        page = deviceClassMapper.selectPage(page, queryWrapper);
        return TableResultData.success(page.getTotal(), page.getRecords());
    }

    /**
     * 新增设备类型
     * @param deviceaddPar
     * @return
     */
    @Override
    public AppResultData deviceClassAdd(DeviceClass deviceaddPar) {
        DeviceClass deviceClass = deviceClassMapper.selectOne(new QueryWrapper<DeviceClass>().eq("code", deviceaddPar.getCode()));
        if (!IsNull.isNullOrEmpty(deviceClass)){
            return AppResultData.errorMessage("该编码设备已存在");
        }
        DeviceClass device = deviceaddPar;
        if (IsNull.isNullOrEmpty(device.getId())) {
            device.setCreateTime(new Date());
            deviceClassMapper.insert(device);
        } else {
            device.setUpdateTime(new Date());
            deviceClassMapper.updateById(device);
        }
        return AppResultData.successMessage("成功");
    }

    /**
     * 删除设备类型
     * @param id
     * @return
     */
    @Override
    public AppResultData deviceClassDel(Integer id) {
        int count = deviceClassMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }
    /**
     * 配置列表
     * @param deviceListPar
     * @return
     */
    @Override
    public TableResultData settingList(DeviceListPar deviceListPar) {
        IPage<MqttSetting> page = new Page<>(deviceListPar.getPage(), deviceListPar.getPageSize());
        QueryWrapper<MqttSetting> queryWrapper = new QueryWrapper<>();
        page = mqttSettingMapper.selectPage(page, queryWrapper);
        return TableResultData.success(page.getTotal(), page.getRecords());
    }

    /**
     * 新增配置
     * @param mqttSetting
     * @return
     */
    @Override
    public AppResultData settingAdd(MqttSetting mqttSetting) {
        if (IsNull.isNullOrEmpty(mqttSetting.getId())) {
            mqttSetting.setCreateTime(new Date());
            mqttSettingMapper.insert(mqttSetting);
        } else {
            mqttSettingMapper.updateById(mqttSetting);
        }
        return AppResultData.successMessage("成功");
    }

    /**
     * 删除配置
     * @param id
     * @return
     */
    @Override
    public AppResultData settingDel(Integer id) {
        int count = mqttSettingMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }


}
